<div id="tieude_1">
		<p>Chỉnh sửa thông tin trẻ</p>
</div>
<div id="noidung_gt">
	<?php
		if (isset($_REQUEST['matre'])) {
			$matre = $_REQUEST['matre'];
			$result = $mysqli->query("select * from tre,phuhuynh,dantoc,tongiao where 
								tre.MA_PH = phuhuynh.MA_PH and
								tre.MA_DT = dantoc.MA_DT and
								tre.MA_TG = tongiao.MA_TG and 
								tre.MA_TRE = '{$matre}'");
			$row=$result->fetch_array();
			$ketqua = $mysqli->query("select * from tre,hoc,lop,loailop where 
								tre.MA_TRE = hoc.MA_TRE and
								hoc.MA_LOP = lop.MA_LOP and
								lop.MA_LOAILOP = loailop.MA_LOAILOP and
								tre.MA_TRE = '{$matre}' and
								hoc.NAM = ".date("Y"));
			$dong = $ketqua->fetch_array();
		}
	?>
    <form name="formnew" id="formnew" method="post">
    	<table border="0" id="bang" class="chu">
            <tr>
            	<td width="40%" ><p>Họ tên trẻ <span>(*)</span></p></td>
                <td width="60%">
                    <input type="text" name="hoten" id="hoten" size="40" value="<?php echo $row['HOTEN_TRE']; ?>"/>
                </td>
            </tr>
            <tr>
            	<td ><p>Ngày sinh <span>(*)</span></p></td>
                <td><input type="date" name="ngaysinh" id="ngaysinh" value="<?php echo $row['NGAYSINH_TRE']; ?>" /></td>
               <script>
  					$(function() {
  						$( "#ngaysinh" ).datepicker();
  					});
  				</script>
            </tr>
            <tr>
            	<td><p>Giới tính  <span>(*)</span></p></td>
                    <td><select name="gioitinh">
                    	<?php 
							if ($row['GIOITINH_TRE'] == "Nam") {
								echo "<option value='Nam' selected>Nam</option>";
								echo "<option value='Nữ'>Nữ</option>";
							} else {
								echo "<option value='Nam'>Nam</option>";
								echo "<option value='Nữ' selected>Nữ</option>";
							}
						?>	 
  						</select>
					</td>
                </tr>
            <tr>
             	<td ><p>Dân tộc <span>(*)</span></p></td>
               	<td><select name="dantoc">
                    	<?php 
							$result1 = $mysqli->query('select * from dantoc');
							while ($row1 = $result1->fetch_array()) {
								if ($row['MA_DT'] == $row1['MA_DT'])
									echo "<option value='{$row1['MA_DT']}' selected>{$row1['TEN_DT']}</option>";
								else echo "<option value='{$row1['MA_DT']}'>{$row1['TEN_DT']}</option>";
							}
						?>	 
  						</select>
				</td>
            </tr>
            <tr>
             	<td ><p>Tôn giáo <span>(*)</span></p></td>
               	<td><select name="tongiao">
                    	<?php 
							$result1 = $mysqli->query('select * from tongiao');
							while ($row1 = $result1->fetch_array()) {
								if ($row['MA_TG'] == $row1['MA_TG'])
									echo "<option value='{$row1['MA_TG']}' selected>{$row1['TEN_TG']}</option>";
								else echo "<option value='{$row1['MA_TG']}'>{$row1['TEN_TG']}</option>";
							}
						?>	 
  						</select>
				</td>
            </tr>
            <tr>
             	<td ><p>Phụ huynh <span>(*)</span></p></td>
               	<td><select name="phuhuynh">
                    	<?php 
							$result1 = $mysqli->query('select * from phuhuynh order by HOTEN_PH');
							while ($row1 = $result1->fetch_array()) {
								if ($row['MA_PH'] == $row1['MA_PH'])
									echo "<option value='{$row1['MA_PH']}' selected>{$row1['HOTEN_PH']}</option>";
								else echo "<option value='{$row1['MA_PH']}'>{$row1['HOTEN_PH']}</option>";
							}
						?>	 
  						</select>
				</td>
            </tr>
            <tr>
             	<td ><p>Lớp học <span>(*)</span></p></td>
               	<td><select name="loailop" onChange="laylop()">
                    	<?php 
							$result1 = $mysqli->query('select * from loailop');
							while ($row1 = $result1->fetch_array()) {
								if ($dong['MA_LOAILOP'] == $row1['MA_LOAILOP'])
									echo "<option value='{$row1['MA_LOAILOP']}' selected>{$row1['TEN_LOAILOP']}</option>";
								else echo "<option value='{$row1['MA_LOAILOP']}'>{$row1['TEN_LOAILOP']}</option>";
							}
						?>	 
  						</select> - 
                     <select name="lophoc">
                    	<?php 
							echo "<option value='{$dong['MA_LOP']}' selected>{$dong['TEN_LOP']}</option>";
						?>	 
  						</select>
				</td>
            </tr>
            <?php
				$result_1 = $mysqli->query("select * from tinhhinhchung where 
								MA_TRE = '{$matre}' and THANG = ".date('m').
								" and NAM =".date('Y'));
				$row_1=$result_1->fetch_array();
			?>
            <tr><td colspan="2"><p><b>Tình hình phát triển tháng hiện tại của trẻ</b></p></td></tr>
            <tr>
            	<td><p>Chiều cao (cm)</span></p></td>
                <td>
                    <input type="text" name="chieucao" id="chieucao" size="20" value="<?php echo $row_1['CHIEUCAO']; ?>"/>
                </td>
            </tr>
            <tr>
            	<td><p>Cân nặng (kg)</span></p></td>
                <td>
                    <input type="text" name="cannang" id="cannang" size="20" value="<?php echo $row_1['CANNANG']; ?>"/>
                </td>
            </tr>
            <?php
				$result_2 = $mysqli->query("select * from co_tn,tiemngua where 
								co_tn.MA_TN = tiemngua.MA_TN and
								co_tn.MA_TRE = '{$matre}' and co_tn.THANG = ".date('m').
								" and co_tn.NAM =".date('Y'));
				$row_2=$result_2->fetch_array();
			?>
            <tr>
             	<td ><p>Bệnh tiêm ngừa</p></td>
               	<td><select name="tiemngua">
                		<option value='none'>Chọn bệnh tiêm ngừa</option>
                    	<?php 
							$result_3 = $mysqli->query('select * from tiemngua');
							while ($row_3 = $result_3->fetch_array()) {
								if ($row_2['MA_TN'] == $row_3['MA_TN'])
									echo "<option value='{$row_3['MA_TN']}' selected >{$row_3['TENBENH_TN']}</option>";
								else echo "<option value='{$row_3['MA_TN']}'>{$row_3['TENBENH_TN']}</option>";
							}
						?>	 
  					</select>
				</td>
            </tr>
            <tr>
            	<td>&nbsp;</td>
                <td id="nut"><input type="submit" name="luu" value="Lưu lại" />
                			<input type="reset" value="Nhập lại" /></td>
            </tr>
            <tr>
            	<td id="tt" colspan="2"><span>(*)</span> Thông tin bắt buộc nhập</td>
            </tr>
    	</table>
	</form>
    <script>
		function createXHR() {
			var xmlhttp;
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return xmlhttp;
		}
		function laylop() {
			var a = document.formnew.loailop.value;
			var xhr = createXHR();
			xhr.open("get","laytenlop.php?maloai=" + a,false);
			xhr.send(null);
			document.formnew.lophoc.innerHTML = xhr.responseText;
		}
	</script>
    <script type="text/javascript">
		$("#formnew").validate({
                rules:{
                    hoten:{
                        required:true,   
                    },
                    ngaysinh:{
                        required:true,
                    },
                },
                messages:{
                    hoten:{
                        required:"Họ tên không được để trống"
                    },
                    ngaysinh:{
                        required:"Ngày sinh không được để trống",
                    },
                }
            });
	</script>
</div>
<?php
	if (isset($_REQUEST['luu'])) {
		if (isset($_REQUEST['hoten']) && isset($_REQUEST['ngaysinh']) && isset($_REQUEST['gioitinh']) 
			&& isset($_REQUEST['dantoc']) && isset($_REQUEST['tongiao']) && isset($_REQUEST['phuhuynh'])) {
			@$mysqli->query("update tre set MA_DT = {$_REQUEST['dantoc']}, 
											MA_PH = '{$_REQUEST['phuhuynh']}', 
											MA_TG = {$_REQUEST['tongiao']}, 
											HOTEN_TRE = '{$_REQUEST['hoten']}', 
											NGAYSINH_TRE = '{$_REQUEST['ngaysinh']}', 
											GIOITINH_TRE = '{$_REQUEST['gioitinh']}'
							where MA_TRE = '{$_REQUEST['matre']}'")
			or die($mysqli->error);
		}
		if (isset($_REQUEST['lophoc']) && $_REQUEST['lophoc'] != "") {
			$sql_1000 = $mysqli->query("select * from hoc where NAM = ".date('Y')." and MA_TRE = '{$_REQUEST['matre']}'");
			if ($sql_1000->num_rows == 0)
				@$mysqli->query("insert into hoc values (".date('Y').", 
									{$_REQUEST['lophoc']}, '{$_REQUEST['matre']}')")
				or die($mysqli->error);
			else @$mysqli->query("update hoc set MA_LOP = {$_REQUEST['lophoc']}
												where MA_TRE = '{$_REQUEST['matre']}' and 
												NAM = ".date('Y'))
			or die($mysqli->error);
		}
		if ((isset($_REQUEST['chieucao']) && $_REQUEST['chieucao'] != '') || 
			(isset($_REQUEST['cannang']) && $_REQUEST['cannang'] != '')) {
			$sql_2000 = $mysqli->query("select * from tinhhinhchung where NAM = ".date('Y')." and THANG = ".date('m')."
																	and MA_TRE = '{$_REQUEST['matre']}'");
			if ($sql_2000->num_rows == 0) 
				@$mysqli->query("insert into tinhhinhchung values ('{$_REQUEST['matre']}',
										".date('m').", ".date('Y').", {$_REQUEST['chieucao']}, {$_REQUEST['cannang']})")
				or die ($mysqli->error);
			else @$mysqli->query("update tinhhinhchung set CHIEUCAO = {$_REQUEST['chieucao']}, 
															CANNANG = {$_REQUEST['cannang']}
															where MA_TRE = '{$_REQUEST['matre']}' and
															THANG = ".date('m')." and 
															NAM = ".date('Y'))
				or die($mysqli->error);
				
		}
		if (isset($_REQUEST['tiemngua']) && $_REQUEST['tiemngua'] != 'none') {
			$sql_3000 = $mysqli->query("select * from co_tn where MA_TRE = '{$_REQUEST['matre']}' and
																	THANG = ".date('m')." and
																	NAM = ".date('Y')."");
			if ($sql_3000->num_rows == 0)
				@$mysqli->query("insert into co_tn values ({$_REQUEST['tiemngua']}, '{$_REQUEST['matre']}', 
															".date('m').", ".date('Y').")")
				or die($mysqli->error);
			else @$mysqli->query("update co_tn set MA_TN = {$_REQUEST['tiemngua']} 
													where MA_TRE = '{$_REQUEST['matre']}' and
													THANG = ".date('m')." and 
													NAM = ".date('Y'))
				or die($mysqli->error);
		}
		echo "<meta http-equiv='refresh' content='0;index.php?cn=ql_tre&id=xemct_tre&matre=".$_REQUEST['matre']."' >";
	}
?>